﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Castle.ActiveRecord;
using System.Text;
using SimplePMT.Data;
using SimplePMT.Components;
using NHibernate.Expression;

namespace SimplePMT.Ajax
{
    public static class Home
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static string GetWorkQueue(int MemberID, string orderField, bool isDescending)
        {
            Task t = new Task();

            StringBuilder queue = new StringBuilder();
            IList<Task> tasks = new List<Task>();
            IList<Task> closedTasks;
            decimal totalHours = 0;

            using (new SessionScope())
            {
                queue.Append("<tr bgcolor=\"#D1D9E0\">\n");
                queue.Append("<td width=75% align=center OnClick=\"javascript:OnClick=sortQueue('Title');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Task Title</b></td>");
                //queue.Append("<td align=center OnClick=\"javascript:OnClick=sortQueue('TaskType');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Type</b></td>\n");
                queue.Append("<td align=center OnClick=\"javascript:OnClick=sortQueue('AssignedDateTime');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Assigned</b></td>\n");
                queue.Append("<td align=center OnClick=\"javascript:OnClick=sortQueue('DueDate');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Due</b></td>\n");
                queue.Append("<td align=center OnClick=\"javascript:OnClick=sortQueue('Priority');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Priority</b></td>\n");
                //queue.Append("<td align=center OnClick=\"javascript:OnClick=sortQueue('ComplexityEstimatedID');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Est. Complexity</b></td>\n");
                queue.Append("</tr>\n");
                Order sortOrder = new Order(orderField, !isDescending);
                tasks = Task.FindAll(sortOrder, new ICriterion[] { Expression.Eq("AssignedMemberID", MemberID), Expression.Eq("ClosedDateTime", Convert.ToDateTime("1/1/1900")), Expression.Eq("IsResolved", false) });
                closedTasks = Task.FindAll(sortOrder, new ICriterion[] { Expression.Eq("AssignedMemberID", MemberID), Expression.Gt("ClosedDateTime", DateTime.Now.AddDays(-3)), Expression.Le("ClosedDateTime", DateTime.Now) });
                
                foreach (Task task in tasks)
                {
                    bool closed = !task.ClosedDateTime.ToShortDateString().Equals("1/1/1900");
                    TaskContainerItem[] itms = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    TaskContainerItem itm = new TaskContainerItem();
                    if (itms.Length > 0)
                    {
                        itm = itms[0];
                    }
                    string assignedDate = "N/A";
                    string dueDate = "N/A";
                    totalHours += task.HoursEstimated;

                    if (task.AssignedDateTime.HasValue)
                    {
                        assignedDate = task.AssignedDateTime.Value.ToShortDateString();
                    }
                    if (task.DueDate.HasValue)
                    {
                        dueDate = task.DueDate.Value.ToShortDateString();
                    }
                    TaskContainerItem[] tcia = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    TaskContainer container = new TaskContainer();
                    if (tcia.Length > 0)
                    {
                        container = tcia[0].TaskContainer;
                    }

                    queue.Append("<tr OnClick=\"openModal('TaskView.aspx?id=" + task.ID + "&cid=" + itm.TaskContainer.ID + "', 'SimplePMT - " + task.Title.Replace("'", "\\'").Replace("\"", "&quot;") + "');\" OnMouseOver=\"this.className='highlightRow';this.style.cursor='pointer';\" OnMouseOut=\"this.className='';\"");
                    if (closed)
                    {
                        queue.Append(" style=\"text-decoration:line-through;\"");
                    }
                    queue.Append(">\n");
                    queue.Append("<td><img src=\"images/" + task.TaskType.Icon + "\"> [" + task.ID + "] " + task.Title + " (<span style=\"font-size:11px;\">" + Common.GetBaseTaskTreeLabel(container.ID) + "</span>)</td>");
                    queue.Append("<td>" + assignedDate + "</td>\n");
                    queue.Append("<td>" + dueDate + "</td>\n");
                    queue.Append("<td style=\"color:#" + task.Priority.Color + ";\"><b>" + task.Priority.Label + "<b></td>\n");
                    //queue.Append("<td align=center>" + TaskComplexity.Find(task.ComplexityEstimatedID).Label + "</td>\n");
                    queue.Append("</tr>\n");
                    queue.Append("<tr><td colspan=4 height=1></td></tr>");

                }
                foreach (Task task in closedTasks)
                {
                    bool closed = !task.ClosedDateTime.ToShortDateString().Equals("1/1/1900");
                    TaskContainerItem[] itms = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    TaskContainerItem itm = new TaskContainerItem();
                    if (itms.Length > 0)
                    {
                        itm = itms[0];
                    }
                    string assignedDate = "N/A";
                    string dueDate = "N/A";
                    //totalHours += task.HoursEstimated;

                    if (task.AssignedDateTime.HasValue)
                    {
                        assignedDate = task.AssignedDateTime.Value.ToShortDateString();
                    }
                    if (task.DueDate.HasValue)
                    {
                        dueDate = task.DueDate.Value.ToShortDateString();
                    }
                    TaskContainerItem[] tcia = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    TaskContainer container = new TaskContainer();
                    if (tcia.Length > 0)
                    {
                        container = tcia[0].TaskContainer;
                    }

                    queue.Append("<tr OnClick=\"openModal('TaskView.aspx?id=" + task.ID + "&cid=" + itm.TaskContainer.ID + "', 'SimplePMT - " + task.Title.Replace("'", "\\'").Replace("\"", "&quot;") + "');\" OnMouseOver=\"this.className='highlightRow';this.style.cursor='pointer';\" OnMouseOut=\"this.className='';\"");
                    if (closed)
                    {
                        queue.Append(" style=\"text-decoration:line-through;\"");
                    }
                    queue.Append(">\n");
                    queue.Append("<td style=\"font-size:12px;\"><img src=\"images/" + task.TaskType.Icon + "\"> [" + task.ID + "] " + task.Title + " (<span style=\"font-size:11px;\">" + Common.GetBaseTaskTreeLabel(container.ID) + "</span>)</td>");
                    queue.Append("<td>" + assignedDate + "</td>\n");
                    queue.Append("<td>" + dueDate + "</td>\n");
                    queue.Append("<td style=\"color:#" + task.Priority.Color + ";\"><b>" + task.Priority.Label + "<b></td>\n");
                    //queue.Append("<td align=center>" + TaskComplexity.Find(task.ComplexityEstimatedID).Label + "</td>\n");
                    queue.Append("</tr>\n");
                    queue.Append("<tr><td colspan=4 height=1></td></tr>");

                }
                if (!totalHours.Equals(0))
                {
                    queue.Append("<tr bgcolor=\"#D1D9E0\"><td colspan=3 align=right>total hours estimated:</td><td>&nbsp;<b>" + totalHours + "</b></td></tr>");
                }
            }
            if (queue.Length == 0)
            {
                return "<table width=\"100%\" cellspacing=0 cellpadding=2><tr><td colspan=5>No open tasks assignments found.</td></tr></table>";
            }
            else
            {
                return "<table width=\"100%\" cellspacing=0 cellpadding=2>\n" + queue.ToString() + "</table>\n";
            }
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static string GetUnassignedQueue(string orderField, bool isDescending)
        {
            Task t = new Task();

            StringBuilder queue = new StringBuilder();
            IList<Task> tasks;

            using (new SessionScope())
            {
                queue.Append("<tr bgcolor=\"#D1D9E0\">\n");
                queue.Append("<td width=84% align=center OnClick=\"javascript:OnClick=sortUnassignedQueue('Title');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Task Title</b></td>");
                queue.Append("<td width=8% align=center OnClick=\"javascript:OnClick=sortUnassignedQueue('DueDate');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Due Date</b></td>\n");
                queue.Append("<td width=8% align=center OnClick=\"javascript:OnClick=sortUnassignedQueue('Priority');\" OnMouseOver=\"this.style.cursor='pointer';\"><b>Priority</b></td>\n");
                queue.Append("</tr>\n");
                Order sortOrder = new Order(orderField, !isDescending);
                tasks = Task.FindAll(sortOrder, new ICriterion[] { Expression.Eq("AssignedMemberID", 0), Expression.Eq("ClosedDateTime", Convert.ToDateTime("1/1/1900")) });
                foreach (Task task in tasks)
                {
                    TaskContainerItem[] itms = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    TaskContainerItem itm = new TaskContainerItem();
                    if (itms.Length > 0)
                    {
                        itm = itms[0];
                    }
                    string assignedDate = "N/A";
                    string dueDate = "N/A";

                    if (task.AssignedDateTime.HasValue)
                    {
                        assignedDate = task.AssignedDateTime.Value.ToShortDateString();
                    }
                    if (task.DueDate.HasValue)
                    {
                        dueDate = task.DueDate.Value.ToShortDateString();
                    }
                    TaskContainerItem[] tcia = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    TaskContainer container = new TaskContainer();
                    if (tcia.Length > 0)
                    {
                        container = tcia[0].TaskContainer;
                    }

                    queue.Append("<tr style=\"border:dotted 1px #869AAE\" OnClick=\"openModal('TaskView.aspx?id=" + task.ID + "&cid=" + itm.TaskContainer.ID + "', 'SimplePMT - " + task.Title.Replace("'", "\\'") + "');\" OnMouseOver=\"this.className='highlightRow';this.style.cursor='pointer';\" OnMouseOut=\"this.className='';\">\n");
                    queue.Append("<td><img src=\"images/" + task.TaskType.Icon + "\"> [" + task.ID + "] " + task.Title + " (<span style=\"font-size:11px;\">in " + Common.GetBaseTaskTreeLabel(container.ID) + "</span>)</td>"); // Common.GetRecursiveTaskTreePath(container)
                    //queue.Append("<td>" + task.TaskType.Label + "</td>\n");
                    queue.Append("<td>" + dueDate + "</td>\n");
                    queue.Append("<td style=\"color: #" + task.Priority.Color + ";\"><b>" + task.Priority.Label + "</b></td>\n");
                    //queue.Append("<td>" + TaskComplexity.Find(task.ComplexityEstimatedID).Label + "</td>\n");
                    queue.Append("</tr>\n");
                    queue.Append("<tr><td colspan=4 height=1></tr>");

                }
            }

            if (queue.Length == 0)
            {
                return "<table width=\"100%\" cellspacing=0 cellpadding=2><tr><td colspan=4>No open tasks assignments found.</td></tr></table>";
            }
            else
            {
                return "<table width=\"100%\" cellspacing=0 cellpadding=2>\n" + queue.ToString() + "</table>\n";
            }
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
        public static string GetTaskHistory(int memberId)
        {

            Task[] tasks = Task.FindAll(Order.Desc("ClosedDateTime"), Expression.Eq("AssignedMemberID", memberId), Expression.Gt("ClosedDateTime", Convert.ToDateTime("1900-01-01 00:00:00.000")));
            StringBuilder queue = new StringBuilder();
            foreach (Task task in tasks)
            {
                try
                {
                    bool closed = !task.ClosedDateTime.ToShortDateString().Equals("1/1/1900");
                    TaskContainerItem[] itms = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    TaskContainerItem itm = new TaskContainerItem();
                    if (itms.Length > 0)
                    {
                        itm = itms[0];
                    }
                    string assignedDate = "N/A";
                    string dueDate = "N/A";

                    if (task.AssignedDateTime.HasValue)
                    {
                        assignedDate = task.AssignedDateTime.Value.ToShortDateString();
                    }
                    if (task.DueDate.HasValue)
                    {
                        dueDate = task.DueDate.Value.ToShortDateString();
                    }
                    TaskContainerItem[] tcia = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    TaskContainer container = new TaskContainer();
                    if (tcia.Length > 0)
                    {
                        container = tcia[0].TaskContainer;
                    }

                    queue.Append("<tr OnClick=\"openModal('TaskView.aspx?id=" + task.ID + "&cid=" + itm.TaskContainer.ID + "', 'SimplePMT - " + task.Title.Replace("'", "\\'").Replace("\"", "&quot;") + "');\" OnMouseOver=\"this.className='highlightRow';this.style.cursor='pointer';\" OnMouseOut=\"this.className='';\"");
                    queue.Append(">\n");
                    queue.Append("<td><img src=\"images/" + task.TaskType.Icon + "\"> [" + task.ID + "] " + task.Title + " (<span style=\"font-size:11px;\">" + Common.GetBaseTaskTreeLabel(container.ID) + "</span>)</td>");
                    queue.Append("<td>" + assignedDate + "</td>\n");
                    queue.Append("<td>" + dueDate + "</td>\n");
                    queue.Append("<td style=\"color:#" + task.Priority.Color + ";\"><b>" + task.Priority.Label + "<b></td>\n");
                    queue.Append("</tr>\n");
                    queue.Append("<tr><td colspan=4 height=1></td></tr>");
                }
                catch { }
            }
            return "<table width=\"100%\" cellspacing=0 cellpadding=2>\n" + queue.ToString() + "</table>\n";
        }
    }
}
